import { message } from 'antd';

import { getAllModules, getDoorList, getPersonalModules, savePersonalModules } from '@/services/notice/homePage';

export default {
    namespace: 'homePage',
    state: {
        personalModules: [],
        allModules: [],
        dataSource: {},
        userLayout: false
    },
    effects: {
        *getPersonalModules({ callback }, { put, call }) {
            const result = yield call(getPersonalModules);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        personalModules: result.data
                    }
                });
                if (callback && typeof callback === 'function') {
                    callback(result.data);
                }
            }
        },
        *getAllModules({ callback }, { put, call }) {
            const result = yield call(getAllModules);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        allModules: result.data
                    }
                });
                if (callback && typeof callback === 'function') {
                    callback(result.data);
                }
            }
        },
        *savePersonalModules({ payload, callback }, { call }) {
            const result = yield call(savePersonalModules, {
                data: payload.params
            });
            if (result && result.code === 10000) {
                // yield put({
                //     type: 'save',
                //     payload: {
                //         personalModules: result.data || []
                //     }
                // });
                // message.success('保存成功');
                if (callback && typeof callback === 'function') {
                    callback(result.data);
                }
            } else {
                message.error(result.msg);
            }
        },
        *getList(_, { put, call }) {
            const result = yield call(getDoorList);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        dataSource: {
                            noticeList: result.data.noticeList,
                            ruleList: result.data.ruleList
                        }
                    }
                });
            }
        },
        *init({ callback }, { put, take, select }) {
            yield put({ type: 'getPersonalModules', payload: {} }); // 触发
            yield take('getPersonalModules/@@end'); // 结束后执行
            yield put({ type: 'getAllModules', payload: {} });
            yield take('getAllModules/@@end');
            const { allModules, personalModules } = yield select(state => state.homePage);
            // enable是false的module，默认要勾选
            const computedAllModules = allModules.map(item => ({
                ...item,
                check: !item.enable || personalModules.some(it => it.code === item.code)
            }));

            const computedPersonalModules = [];
            // 如果默认模块减少了，减去
            personalModules.forEach(item => {
                if (allModules.some(it => it.code === item.code)) computedPersonalModules.push(item);
            });

            // 如果默认模块增加了，添加
            // 默认模块codes
            const defaultCodes = computedAllModules.filter(item => item.check).map(item => item.code);
            defaultCodes.forEach(item => {
                if (!personalModules.some(it => it.code === item)) {
                    computedPersonalModules.push({
                        position: 'left',
                        code: item,
                        sort: computedPersonalModules.length + 1
                    });
                }
            });

            yield put({
                type: 'save',
                payload: {
                    personalModules: computedPersonalModules,
                    allModules: computedAllModules
                }
            });

            if (callback) {
                callback(computedPersonalModules);
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
